const reqInterface = require("../../../api/models");
const {
  imagePrefix
} = require("../../../utils/dataHelper");
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    image: {
      type: String,
      value: ''
    },
    uploadTxt: String,
    need_preview: {
      type: Boolean,
      value: false
    }
  },

  observers: {
    'image': function (val) {
      let self = this;
      if (val) {
        self.setData({
          imageUrl: val
        })
      }
    }
  },

  lifetimes: {
    attached() {
      let api = __wxConfig.envVersion;
      switch (api) {
        case 'develop': // 开发服
          this.setData({
            api: 'https://rcsc-test.jcjob.cn/api'
          })
          break;
        case 'trial': // 体验服
          this.setData({
            api: 'https://rcsc-test.jcjob.cn/api'
          })
          break;
        default: // 正式服
          this.setData({
            api: 'https://admin1.jcjob.cn/api'
          })
      }
    }
  },

  data: {
    styleCss: 'padding: 38rpx 0; line-height: 36rpx font-size: 28rpx; border-bottom: 1rpx dashed #f0f0f0;',
    api: '',
    imageUrl: '',
    header: {
      Authorization: wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    uploadSuccess(e) {
      let self = this;
      let res = JSON.parse(e.detail.file.res.data)
      self.setData({
        image: imagePrefix() + res.data.filename
      })
      self.triggerEvent('sumbitImageInfo', res.data.filename)
    },

    preview(e) {
      wx.previewImage({
        current: e.currentTarget.dataset.src, // 当前显示图片的http链接
        urls: [e.currentTarget.dataset.src] // 需要预览的图片http链接列表
      })
    },
  }
})
